﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.SiteLibraryManage;
using YunSpace.Domain.IRepository.SiteLibraryManage;
using YunSpace.Repository.SiteLibraryManage;
using System.Collections.Generic;
using System.Linq;
using System;
using YunSpace.Data.Extensions;
using YunSpace.Data;
#endregion

namespace YunSpace.Application.SiteLibraryManage
{
    public class PriceScheduleApp
    {
        private MySqlDbHelper dbHelper = new MySqlDbHelper();
        private IPriceScheduleRepository service = new PriceScheduleRepository();

        public List<PriceScheduleEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<PriceScheduleEntity>();
            var queryParam = queryJson.ToJObject();
            // if (!queryParam["keyword"].IsEmpty())
            // {
            //     string keyword = queryParam["keyword"].ToString();
            //     expression = expression.And(t => t.F_Account.Contains(keyword));
            // }
            // if (!queryParam["timeType"].IsEmpty())
            // {
            //     string timeType = queryParam["timeType"].ToString();
            //     DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
            //     DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
            //     switch (timeType)
            //     {
            //         case "1":
            //             break;
            //         case "2":
            //             startTime = DateTime.Now.AddDays(-7);
            //             break;
            //         case "3":
            //             startTime = DateTime.Now.AddMonths(-1);
            //             break;
            //         case "4":
            //             startTime = DateTime.Now.AddMonths(-3);
            //             break;
            //         default:
            //             break;
            //     }
            //     expression = expression.And(t => t.F_Date >= startTime && t.F_Date <= endTime);
            // }
            return service.FindList(expression, pagination);
        }
        public List<PriceScheduleEntity> GetPriceScheduleList(string keyValue)
        {
            using (var db = new RepositoryBase())
            {
                string strSql = @"select * from Site_PriceSchedule where F_SpaceId='" + keyValue + "' and F_DeleteMark<>1 order by F_ScheduleDate;";
                var data = db.FindList<PriceScheduleEntity>(strSql);
                return data;
            }
        }

        public PriceScheduleEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void DeleteDateForm(string sDate, string eDate)
        {
            DateTime startTime = sDate.ToString().ToDate();
            DateTime endTime = eDate.ToString().ToDate();
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            string modUserId = "";
            if (LoginInfo != null)
            {
                modUserId = LoginInfo.UserId;
            }
            string sql = @"UPDATE Site_PriceSchedule set F_DeleteMark=1 ,F_DeleteTime='"+ DateTime.Now + "',F_DeleteUserId='"
                + modUserId + "' where F_ScheduleDate>='"+ startTime + "' and F_ScheduleDate<='"+ endTime + "'";
            dbHelper.Execute(sql);
        }

        public void SubmitList( List<PriceScheduleEntity> list) {

            string sql = "";
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Create();
                sql += "insert into Site_PriceSchedule(F_Id,F_SiteId,F_SpaceId,F_ScheduleDate,F_YunPrice,F_MarketPrice,F_FloorPrice,F_CreatorTime,F_CreatorUserId)values('"
                    + list[i].F_Id + "','"
                    + list[i].F_SiteId + "','"
                    + list[i].F_SpaceId + "','"
                    + list[i].F_ScheduleDate + "','"
                    + list[i].F_YunPrice + "','"
                    + list[i].F_MarketPrice + "','"
                    + list[i].F_FloorPrice + "','"
                    + list[i].F_CreatorTime + "','"
                    + list[i].F_CreatorUserId + "');";
            }
            dbHelper.Execute(sql);
        }

        public void SubmitForm(PriceScheduleEntity pricescheduleEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                pricescheduleEntity.Modify(keyValue);
                service.Update(pricescheduleEntity);
            }
            else
            {
                pricescheduleEntity.Create();
                // pricescheduleEntity.F_Category = 2;
                service.Insert(pricescheduleEntity);
            }
        }
    }
}

